package com.lzhl.buildjar.ui.support;

import java.io.File;
import java.util.LinkedList;
import java.util.List;

/**
 * @author 李柱良(@柱子的日月)
 * @date 2016/2/2
 */
public class ClassProvider implements SuggestProvider {

    private String classPath;

    public ClassProvider() {
    }

    public ClassProvider(String classPath) {
        this.classPath = classPath;
    }

    public void setClassPath(String classPath) {
        this.classPath = classPath;
    }

    public String getClassPath() {
        return this.classPath;
    }

    public List<String> suggest(String text) {
        LinkedList<String> suggests = new LinkedList<String>();

        String classPath = getClassPath();
        File fClassPath = new File(classPath);
        if(!fClassPath.isDirectory() || classPath.endsWith(":")) {
            return suggests;
        }
        classPath = fClassPath.getAbsolutePath();

        String path =classPath;
        text = text.trim();
        if(text.length() > 0) {
            path += File.separator + text;
        }
        File file = new File(path);
        File parent;
        if(path.equals(classPath) || text.endsWith(File.separator)) {
            parent = file;
        }
        else {
            parent = file.getParentFile();
        }

        if(parent != null) {
            File[] files = parent.listFiles();
            if (files != null) {
                int len = classPath.length() + File.separator.length();
                for (File aFile : files) {
                    if(aFile.getAbsolutePath().toLowerCase().startsWith(path.toLowerCase())) {
                        suggests.addLast(aFile.getAbsolutePath().substring(len));
                    }
                }
            }
        }
        return suggests;
    }
}
